local skel = fk.CreateSkill {
  name = "rmt__luedong",
}

Fk:loadTranslationTable{
  ["rmt__luedong"] = "略懂",
  [":rmt__luedong"] = "摸牌阶段，你可以改为摸至多你上次弃置牌数两倍的牌；弃牌阶段，你可以改为弃置至少你上次摸牌数两倍的牌。",

  ["#rmt__luedong-draw"] = "略懂：现在是摸牌阶段，是否要更改摸牌数？",
  ["#rmt__luedong-discard"] = "略懂：现在是弃牌阶段，是否要改为弃置至少%arg张牌？",

  ["$rmt__luedong1"] = "据图谋不轨，今奉诏索命。",
  ["$rmt__luedong2"] = "休妄论芍陂之战，当诛之。",
}

skel:addEffect(fk.DrawNCards, {
  anim_type = "drawcard",
  audio_index = 1,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local n = 0
      player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == player and move.moveReason == fk.ReasonDiscard and move.proposer == player then
            n = n + #move.moveInfo
          end
        end
        if n > 0 then
          return true
        end
      end, 1)
      if n > 0 then
        event:setCostData(self, {max = 2 * n})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local choices = {}
    for i = 0, event:getCostData(self).max, 1 do
      table.insert(choices, tostring(i))
    end
    table.insert(choices, "Cancel")
    local choice = player.room:askToChoice(player, { choices = choices, skill_name = skel.name, prompt = "#rmt__luedong-draw"})
    if choice ~= "Cancel" then
      event:setCostData(self, {num = tonumber(choice)})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    data.n = event:getCostData(self).num
  end,
})

skel:addEffect(fk.EventPhaseProceeding, {
  anim_type = "negative",
  audio_index = 2,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      if not data.phase_end and player.phase == Player.Discard and not player:isNude() then
        local n = 0
        player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
              n = n + #move.moveInfo
            end
          end
          if n > 0 then
            return true
          end
        end, 1)
        if n > 0 then
          event:setCostData(self, {min = 2 * n})
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local min = event:getCostData(self).min
    local cards = player.room:askToDiscard(player, {
      min_num = min, max_num = min + 9999, include_equip = true, skill_name = skel.name, cancelable = true,
      prompt = "#rmt__luedong-discard:::"..min, skip = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    data.phase_end = true
    player.room:throwCard(event:getCostData(self).cards, skel.name, player, player)
  end,
})

return skel
